<?php

namespace App\Http\Controllers\Login;

use App\Http\Controllers\Base\Base;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class Login extends Controller
{
    private $baseClass;

    function __construct()
    {
        $this->baseClass = new Base();
    }

    public function loadLogin (){
        return view('login');
    }

    public function login(){
        $user = $this->baseClass->htmlPurifier('user');
        $pass = $this->baseClass->htmlPurifier('pass');
        $data = array();
        if ($user != '' && $pass != ''){
            $result = DB::table($this->baseClass->userTable)
                ->where('user', '=', $user)
                ->where('pass', '=', $pass)
                ->where('can_login', '=', 0)
                ->get();
            if (count($result) >= 1){
                $item = $result[0];
                $userInfo = array();
                $userInfo['uid'] = $item->uid;
                Session::put('userInfo', $userInfo);
                $status = 0;
                $content = '登录成功';
            }
            else {
                $status = 201;
                $content = '帐号密码不正确';
            }
        }
        else {
            $status = 101;
            $content = '参数不能为空';
        }
        return $this->baseClass->returnJson($status, $content, $data);
    }
}
